package com.yhsimon.rabbitmq.work.round;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Producer {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setVirtualHost("/");
        connectionFactory.setHost("192.168.100.128");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");

        Connection connection=null;
        Channel channel=null;
        try {
            connection = connectionFactory.newConnection("Producer");

            channel=connection.createChannel();

            for (int i = 0; i < 20; i++) {
                String msg="goods"+i;

                channel.basicPublish("","queue-work-round",null,msg.getBytes());

            }
            System.out.println("消息发送成功");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("发送消息出现异常");
        }finally {
            if(channel!=null&&channel.isOpen()){
                try {
                    channel.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if(connection!=null&&connection.isOpen()){
                try {
                    connection.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
